package com.sunsun_sunsun_home;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.sunsun.MainHomeEnglishActivity;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.controller.LoginController;
import com.itboye.sunsun.custome.RoundImage;
import com.itboye.sunsun.message.ui.MyMessageActivity;
import com.itboye.sunsun.person.ui.MyOrderActivity;
import com.itboye.sunsun.person.ui.PersonSettingActivity;
import com.itboye.sunsun.person.ui.SttingActivity;
import com.itboye.sunsun.support.BaseFragment;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.utils.XImageLoader;
import com.itboye.sunsunhome.R;

/**
 * @date
 * @author
 * @description 侧边栏菜单
 */
public class LeftFragment extends BaseFragment implements OnClickListener {
	private View todayView;

	private TextView tvLastlist, tvname, tvaq;

	private TextView tvaeeee, tvaee;
	private RoundImage roundview;
	
	private RelativeLayout realypoutTop,linyoutTop;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.layout_menu, null);

		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		findViews();
		
		IntentFilter intentFilterAvart = new IntentFilter("changeavart");
		getActivity().registerReceiver(avartBroadcastReceiver,
				intentFilterAvart);
		
		
		
		IntentFilter intentFilterAvartnecheng = new IntentFilter("necheng");
		getActivity().registerReceiver(avartBroadcastReceivernehneg,
				intentFilterAvartnecheng);
	}
	BroadcastReceiver avartBroadcastReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			String resultData = (String) SPUtils.get(App.ctx, null,
					SPContants.AVATAR_URL, "");

			findViews();
			System.out.println("<><>>>>>>>>>>>>>>>>>>>>>>>>"+resultData);
			XImageLoader.load(resultData, roundview);
		};
	};

	
	BroadcastReceiver avartBroadcastReceivernehneg = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			String resultData = (String) SPUtils.get(App.ctx, null,
					SPContants.NICK, "");

			findViews();
			tvname.setText(resultData);
		};
	};

	public void findViews() {
		String uri = (String) SPUtils.get(App.ctx, null, SPContants.AVATAR_URL,
				"");

		XImageLoader.load(uri, roundview);

		String phonenumber = (String) SPUtils.get(App.ctx, null,
				SPContants.NICK, "");
		String AQ_num = (String) SPUtils.get(App.ctx, null, SPContants.AQ_NUM,
				"");
		tvname.setText("" + phonenumber);
		if (AQ_num.equals("")) {
			tvaq.setText(R.string.no_devices);
			tvaeeee.setVisibility(View.GONE);
			tvaee.setVisibility(View.GONE);
		} else
			tvaeeee.setVisibility(View.VISIBLE);
		tvaee.setVisibility(View.VISIBLE);
		tvaq.setText(AQ_num);

		todayView = getActivity().findViewById(R.id.tvToday);

		tvLastlist = (TextView) getActivity().findViewById(R.id.tvLastlist);
		todayView.setOnClickListener(this);
		tvLastlist.setOnClickListener(this);

	}

	//
	// @Override
	// public void onDestroyView() {
	// super.onDestroyView();
	// }
	//
	// @Override
	// public void onDestroy() {
	// super.onDestroy();
	// }

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.linyoutTop:
			onLogon();
//			LoginController.onPersonalsettingClick(getActivity(), null);
			break;
		case R.id.realypoutTop:
			onLogon();
//			LoginController.onPersonalsettingClick(getActivity(), null);
			break;
		case R.id.tvToday: // 今日onMessge
//			Intent intent2 = new Intent(getActivity(), MyMessageActivity.class);
//			startActivity(intent2);
//			 LoginController.onMessge(getActivity(), null);
			onString();
			break;
		case R.id.tvLastlist:

//			 LoginController.onSttingClick(getActivity(), null);

			Intent intent3 = new Intent(getActivity(), SttingActivity.class);
			startActivity(intent3);
			break;

		default:
			break;
		}
		// if (newContent != null) {
		// switchFragment(newContent, title);
		// }
	}
	// /**
	// * 切换fragment
	// * @param fragment
	// */
	// private void switchFragment(Fragment fragment, String title) {
	// if (getActivity() == null) {
	// return;
	// }
	// if (getActivity() instanceof MainHomeEnglishActivity) {
	// MainHomeEnglishActivity fca = (MainHomeEnglishActivity) getActivity();
	// fca.switchConent(fragment, title);
	// }
	// }
	
	public  void onLogon(){
		String UID = (String) SPUtils.get(App.ctx, null, SPContants.USER_ID,
				"");
		
		if(UID=="" || UID==null){
			System.out.println("<><><><>"+UID);
			LoginController.onPersonalsettingClick(getActivity(), null);
		}else{
			Intent intent8 = new Intent();
			intent8.setClass(App.ctx, PersonSettingActivity.class);
			intent8.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent8);
		}
	}
	
	
	public  void onString(){
		String UID = (String) SPUtils.get(App.ctx, null, SPContants.USER_ID,
				"");
		
		if(UID=="" || UID==null){
			 LoginController.onMessge(getActivity(), null);
		}else{
			Intent intent2 = new Intent(getActivity(), MyMessageActivity.class);
			startActivity(intent2);
		}
	}

}
